package com.bibrApp.difu.actvity.main_mvp;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;
import android.widget.Toast;

import com.bibrApp.difu.DfApp;
import com.bibrApp.difu.R;
import com.bibrApp.difu.actvity.login_mvp.LoginActivity;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.base.BasePermissionActivity;
import com.bibrApp.difu.fragment.home_mvp.HomeFragment;
import com.bibrApp.difu.fragment.jiedai_mvp.JiedaiFragment;
import com.bibrApp.difu.fragment.wallet_mvp.WalletFragment;
import com.bibrApp.difu.utils.ToastUtils;
import com.bibrApp.difu.utils.UIUtils;
import com.bibrApp.difu.widget.HomeViewPager;
import com.gyf.barlibrary.ImmersionBar;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BasePermissionActivity<MainPresenter> implements IMainView {

    public static final int REQUEST_CODE = 0x123;
    @BindView(R.id.tab_home)
    TextView tab_home;
    @BindView(R.id.tab_loan)
    TextView tab_loan;
    @BindView(R.id.tab_wallet)
    TextView tab_wallet;
    @BindView(R.id.home_view_pager)
    HomeViewPager viewPager;
    private static final String[] tabs = {"首页", "借贷", "钱包"};
    Map<String, Fragment> fragments = new HashMap<>();
    private DfApp app;

    @Override
    protected void initPresenter() {
        presenter = new MainPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void success(String msg) {

    }

    @Override
    public void initView() {
        app = (DfApp) getApplication();
        initViewPager();
        changePage(0);
    }

    @OnClick(R.id.tab_home)
    public void clickHome() {
        changePage(0);
    }

    @OnClick(R.id.tab_loan)
    public void clickLoan() {
        if (TextUtils.isEmpty(app.getToken())) {
            ToastUtils.ShowToast(this, "请登录");
            startActivity(new Intent(this, LoginActivity.class));
            return;
        }
        changePage(1);
    }

    @OnClick(R.id.tab_wallet)
    public void clickWallet() {
        if (TextUtils.isEmpty(app.getToken())) {
            ToastUtils.ShowToast(this, "请登录");
            startActivity(new Intent(this, LoginActivity.class));
            return;
        }
        changePage(2);
    }

    public void initViewPager() {
        /*
         * 1.FragmentPagerAdapter, FragmentStatePagerAdapter 的区别
         *     FragmentPagerAdapter 中, 即使fragment不可见了, 他的视图可能会 destory(执行 onViewDestory, 是否执行与setOffscreenPageLimit 方法设置的值有关), 但是他的实例仍然会存在于内存中. 当较多的fragment时, 会占用较大的内存.
         *     FragmentSatePagerAdapter 中, 当fragment不可见时, 可能会将fragment的实例也销毁(执行 onDestory, 是否执行与setOffscreenPageLimit 方法设置的值有关). 所以内存开销会小些, 适合多fragment的情形.
         *
         *     FragmentPagerAdapter当超过设置的setOffscreenPageLimit大小调用onViewDestory
         *     FragmentSatePagerAdapter当超过设置的setOffscreenPageLimit大小调用onDestory
         */
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return tabs.length;
            }

            @NonNull
            @Override
            public Fragment getItem(int position) {
                Fragment fragment = fragments.get(tabs[position]);
                if (fragment != null) {
                    return fragment;
                }
                switch (position) {
                    case 0:
                        fragment = new HomeFragment();
                        break;
                    case 1:
                        fragment = new JiedaiFragment();
                        break;
                    case 2:
                        fragment = new WalletFragment();
                        break;
                }
                fragments.put(tabs[position], fragment);
                return fragment;
            }
        });
        viewPager.setNoScroll(true);
        viewPager.setOffscreenPageLimit(2);
    }

    public void changePage(int index) {
        if (index == viewPager.getCurrentItem()) {
            return;
        }
        viewPager.setCurrentItem(index, false);
        switch (index) {
            case 0:

                tab_home.setTextColor(getResources().getColor(R.color.navbar_selected_color));
                tab_loan.setTextColor(getResources().getColor(R.color.navbar_nomal_color));
                tab_wallet.setTextColor(getResources().getColor(R.color.navbar_nomal_color));

                UIUtils.setDrawableTop(tab_home, R.mipmap.icon_home_press);
                UIUtils.setDrawableTop(tab_loan, R.mipmap.icon_loan_normal);
                UIUtils.setDrawableTop(tab_wallet, R.mipmap.icon_wallet_normal);
                break;

            case 1:
                tab_home.setTextColor(getResources().getColor(R.color.navbar_nomal_color));
                tab_loan.setTextColor(getResources().getColor(R.color.navbar_selected_color));
                tab_wallet.setTextColor(getResources().getColor(R.color.navbar_nomal_color));

                UIUtils.setDrawableTop(tab_home, R.mipmap.icon_home_normal);
                UIUtils.setDrawableTop(tab_loan, R.mipmap.icon_loan_press);
                UIUtils.setDrawableTop(tab_wallet, R.mipmap.icon_wallet_normal);
                break;

            case 2:
                tab_home.setTextColor(getResources().getColor(R.color.navbar_nomal_color));
                tab_loan.setTextColor(getResources().getColor(R.color.navbar_nomal_color));
                tab_wallet.setTextColor(getResources().getColor(R.color.navbar_selected_color));

                UIUtils.setDrawableTop(tab_home, R.mipmap.icon_home_normal);
                UIUtils.setDrawableTop(tab_loan, R.mipmap.icon_loan_normal);
                UIUtils.setDrawableTop(tab_wallet, R.mipmap.icon_wallet_press);
                break;
        }
    }

    //返回键处理
    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}
